/*
 * jfrNewTeam1.java
 *
 * Created on 3 août 2008, 17:59
 */
package bbos.General.Views.Team;

import bbos.General.Model.mPlayer;
import bbos.General.Model.mTeam;
import bbos.General.Views.Team.jpnTeam;
import bbos.mBBoS;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JOptionPane;

/**
 *
 * @author  frederic
 */
public class jdgTeam extends javax.swing.JDialog
{

    jpnTeam jpnteam;
    mTeam _team;
    boolean _addTeam = false;

    /** Creates new form jfrNewTeam1 */
    public jdgTeam()
    {
        initComponents();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        DisplayMode dmode = gs.getDisplayMode();

        if (dmode != null)
        {
            int screenWidth = dmode.getWidth();
            int screenHeight = dmode.getHeight();
            this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
        }

        _team = new mTeam();
        jpnteam = new jpnTeam(_team, jtmTeamPlayers.NEW_VIEW);
        add(jpnteam, java.awt.BorderLayout.CENTER);
        _addTeam = true;
        
        
    }

    public jdgTeam(mTeam team, int view)
    {
        initComponents();

        this.setMinimumSize(new Dimension(930, 600));

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        DisplayMode dmode = gs.getDisplayMode();

        if (dmode != null)
        {
            int screenWidth = dmode.getWidth();
            int screenHeight = dmode.getHeight();
            this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
        }

        _team = team;

        jpnteam = new jpnTeam(_team, view);
        add(jpnteam, java.awt.BorderLayout.CENTER);

        _addTeam = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jPanel1 = new javax.swing.JPanel();
        jbtOK = new javax.swing.JButton();
        jbtCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Team");
        setMinimumSize(new java.awt.Dimension(800, 600));
        setModal(true);

        jbtOK.setText("OK");
        jbtOK.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jbtOKActionPerformed(evt);
            }
        });
        jPanel1.add(jbtOK);

        jbtCancel.setText("Cancel");
        jbtCancel.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jbtCancelActionPerformed(evt);
            }
        });
        jPanel1.add(jbtCancel);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents
    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jbtCancelActionPerformed
    {//GEN-HEADEREND:event_jbtCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jbtCancelActionPerformed

    private void jbtOKActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jbtOKActionPerformed
    {//GEN-HEADEREND:event_jbtOKActionPerformed
        if (_addTeam)
        {

            if (_team.getName().equals(""))
            {
                JOptionPane.showMessageDialog(this, "Empty team name.", "Empty field", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (_team.getPlayers().size() < 11)
            {
                JOptionPane.showMessageDialog(this, "Your team have less han 11 players ", "Wrong players numbers", JOptionPane.ERROR_MESSAGE);
                return;
            }

            for (int i = 0; i < 16; i++)
            {
                mPlayer player = _team.getPlayer(i + 1);
                if (player != null)
                {
                    if (player.getName().equals(""))
                    {
                        JOptionPane.showMessageDialog(this, "One of your player is unnamed", "Wrong player name", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
            }

            int result = JOptionPane.showConfirmDialog(this, "You will add your team. Do tou want to continue", "", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION)
            {
                this.setVisible(false);
                mBBoS.getSingleton().addTeam(_team);
            }
        }
        else
        {
            setVisible(false);
        }
    }//GEN-LAST:event_jbtOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtOK;
    // End of variables declaration//GEN-END:variables
}
